import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;


public class Projeto {
	
	public Projeto() {
		
		int conjunto = 1;
		
		try {
			
			BufferedReader br = new BufferedReader(new FileReader("Entrada.in"));
			BufferedWriter bw = new BufferedWriter(new FileWriter("Saida.out"));
			
			String linha;
			
			while(br.ready()){
				
				GLC g = new GLC();
				
				linha = br.readLine();
				g.variaveis = linha.split(",");
				
				linha = br.readLine();
				g.alfabeto = linha.split(",");
				
				String[] regras = br.readLine().split(",");
				
				for(String regra : regras){
					
					String var = regra.split("->")[0];
					String[] r = regra.split("->")[1].replace('|', ',').split(",");
					
					Vector<String> rs = new Vector<String>();
					
					for(String str : r){
						rs.add(str);
					}
					
					g.regras.put(var, rs);
				}
				g.inicial = br.readLine();
				
				int i = Integer.parseInt(br.readLine());
				
				System.out.println("conjunto#" + conjunto);
				bw.write("conjunto#" + conjunto);
				
				for(;i > 0; i--){
					
					String str = br.readLine();
					
					if(g.testarCadeia(str)){
						System.out.println("aceita");
						bw.write("aceita");
					} else {
						System.out.println("rejeita");
						bw.write("rejeita");
					}
				}
				
				br.readLine();
				bw.write("\n");
				System.out.println();
				
				conjunto++;
			}
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		new Projeto();
	}
	
}
